<template>
  <el-dialog :title="form.facilityId ? '更改设施' : '增加设施'" width="600" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="90px" v-loading="loading">
      <el-form-item label="设施名称" prop="facilityName">
        <el-input v-model="form.facilityName" placeholder="请输入设施名称"/>
      </el-form-item>
      <el-form-item label="设施类型" prop="facilityType">
        <el-select v-model="form.facilityType" placeholder="请选择设施类型" clearable>
          <el-option
              v-for="item in facilityDict"
              :key="item.id"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="facilityResponsePeople">
        <el-input v-model="form.facilityResponsePeople" placeholder="请输入负责人"/>
      </el-form-item>
      <el-form-item label="联系人" prop="facilityContactPeople">
        <el-input v-model="form.facilityContactPeople" placeholder="请输入联系人"/>
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input v-model="form.contactPhone" placeholder="请输入联系电话"/>
      </el-form-item>
      <el-form-item label="详细地址" prop="facilityLocation">
        <el-input v-model="form.facilityLocation" placeholder="请输入详细地址"/>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" maxlength="150" rows="3" v-model="form.remark" placeholder="请输入备注"/>
      </el-form-item>
    </el-form>
    <template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" >
import {useMessage} from '/@/hooks/message';
import {addFacility,updateFacility,getFacilityById} from "/@/api/facility/facility";

import { getDicts } from '/@/api/admin/dict';

const facilityDict = ref([]);

const loadFacilityDict = async () => {
    const res = await getDicts('facility_type');
    facilityDict.value = res.data || [];
};

onMounted(() => {
  loadFacilityDict();
});


// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);


// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);

// 提交表单数据
const form = reactive({
  facilityId: 0,
  facilityType: '',
  facilityName: '',
  facilityResponsePeople: '',
  facilityContactPeople: '',
  contactPhone: '',
  facilityLocation: '',
  remark: '',
  delFlag: '',
  createTime: '',
  createBy: '',
  updateTime: '',
  updateBy: '',
});

// 定义校验规则
const dataRules = ref({
  facilityType: [
    {required: true, message: '设施类型不能为空', trigger: 'blur'},
    {
      trigger: 'blur',
    },
  ],
  facilityName: [
    {required: true, message: '设施名称不能为空', trigger: 'blur'},
  ],
  facilityResponsePeople: [
    {required: true, message: '设施负责人不能为空', trigger: 'blur'},
  ],
  facilityContactPeople: [
    {required: true, message: '设施联系人不能为空', trigger: 'blur'},
  ],
  facilityLocation: [
    {required: true, message: '详细地址不能为空', trigger: 'blur'},
  ],
  contactPhone: [{required: true, message: '联系电话不能为空', trigger: 'blur'}],
});

// 打开弹窗
const openDialog = (id: number) => {
  visible.value = true;

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取Post信息
  if (id) {
    form.facilityId = id;
    getFacilityData(id);
  } else {
    form.facilityId = 0;
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    form.facilityId ? await updateFacility(form) : await addFacility(form);
    useMessage().success((form.facilityId ? '修改成功' : '增加成功'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// 初始化表格数据
const getFacilityData = (id: number) => {
  // 获取部门数据
  getFacilityById(id).then((res: any) => {
    Object.assign(form, res.data);
  });
};

// 暴露变量
defineExpose({
  openDialog,
});
</script>
